package com.csii.adhoc.board.serviceImpl;

import com.csii.adhoc.board.BoardDtlPo;
import com.csii.adhoc.board.filter.FilterUnitPo;
import com.csii.adhoc.board.filter.FilterUnitVo;
import com.csii.adhoc.board.mapper.FilterUnitMapper;
import com.csii.adhoc.board.service.BoardDtlService;
import com.csii.adhoc.board.service.FilterUnitService;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.id.UidGenerator;
import com.csii.common.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 过滤组件接口实现类
 * @author: panChuanJiang
 * @create: 2021-02-04 15:08
 */
@Slf4j
@Service("filterUnitService")
public class FilterUnitServiceImpl implements FilterUnitService {

    @Resource
    private FilterUnitMapper filterUnitMapper;
    @Resource
    private UidGenerator uidGenerator;
    @Resource
    private BoardDtlService boardDtlService;
    @Override
    @Transactional
    public void FilterUnitMaintain(FilterUnitVo vo, String optMode) {
        switch (optMode) {
            case CommonConstant.OPT_MODE_ADD:
                vo.setCreatedBy(vo.getUpdatedBy())
                        .setCreatedDt(vo.getUpdatedDt());
                insertOneFilterUnit(vo);
                break;
            case CommonConstant.OPT_MODE_DELETE:
                deleteFilterUnitByPrimaryKey(vo.getUnitId());
                break;
            case CommonConstant.OPT_MODE_UPDATE:
                updateFilterUnitByPrimaryKey(vo);
                break;
            default:
                log.error("optMode值不合法:" + optMode);
                throw new ParamValidityException("optMode值不合法:" + optMode);
        }
    }

    @Transactional
    public void updateFilterUnitByPrimaryKey(FilterUnitPo filterUnitPo) {
        filterUnitMapper.updateFilterUnitByPrimaryKey(filterUnitPo);
    }

    @Transactional
    public void deleteFilterUnitByPrimaryKey(String unitId) {
        filterUnitMapper.deleteFilterUnitByPrimaryKey(unitId);
    }

    @Transactional
    public void insertOneFilterUnit(FilterUnitVo vo) {
        if (StringUtils.isEmpty(vo.getUnitId())) {
            vo.setUnitId(uidGenerator.getUID() + "");
        }
        filterUnitMapper.insertOneFilterUnit(vo);
        BoardDtlPo po = new BoardDtlPo();
        po.setCreatedDt(vo.getCreatedDt())
                .setCreatedBy(vo.getCreatedBy())
                .setUpdatedDt(vo.getUpdatedDt())
                .setUpdatedBy(vo.getUpdatedBy())
                .setUnitId(vo.getUnitId())
                .setBoardId(vo.getBoardId())
                .setUnitType("2")
                .setTitleId("未命名组件")
                .setVaxis(vo.getVaxis())
                .setLength(vo.getLength())
                .setHaxis(vo.getHaxis())
                .setWidth(vo.getWidth());
    boardDtlService.insertOneBoardDtl(po);
    }

    @Override
    public List<FilterUnitPo> selectListFilterUnit(FilterUnitPo filterUnitPo) {
        return filterUnitMapper.selectListFilterUnit(filterUnitPo);
    }

    @Override
    public FilterUnitPo selectOneFilterUnit(String unitId) {
        return filterUnitMapper.selectFilterUnitByPrimaryKey(unitId);
    }


}
